package com.atlassian.extras.core.bamboo;

import com.atlassian.extras.api.LicenseEdition;
import com.atlassian.extras.api.LicenseType;
import com.atlassian.extras.api.Product;
import com.atlassian.extras.api.bamboo.BambooLicense;
import com.atlassian.extras.common.LicenseTypeAndEditionResolver;
import com.atlassian.extras.common.util.LicenseProperties;
import com.atlassian.extras.core.DefaultProductLicense;

class DefaultBambooLicense extends DefaultProductLicense implements
		BambooLicense {
	private static final int MAX_REMOTE_AGENTS_NONE = 0;
	private static final int MAX_REMOTE_AGENTS_STANDARD = 1;
	private static final int MAX_REMOTE_AGENTS_PROFESSIONAL = 10;
	private static final int MAX_REMOTE_AGENTS_ENTERPRISE = 25;
	private static final int MAX_REMOTE_AGENTS_UNLIMITED = 100;
	private static final int MAX_LOCAL_AGENTS_BASIC = 1;
	private static final int MAX_LOCAL_AGENTS_UNLIMITED = -1;
	private static final int MAX_PLANS_STARTER = 10;
	private static final int MAX_PLANS_UNLIMITED = -1;
	private final int maximumNumberOfRemoteAgents;
	private final int maximumNumberOfLocalAgents;
	private final int maximumNumberOfPlans;
	private final LicenseEdition licenseEdition;

	DefaultBambooLicense(Product product, LicenseProperties licenseProperties) {
		super(product, licenseProperties);
		this.licenseEdition = LicenseTypeAndEditionResolver
				.getLicenseEdition(licenseProperties
						.getProperty("LicenseEdition"));

//		this.maximumNumberOfRemoteAgents = calculateRemoteAgents(licenseProperties);
//		this.maximumNumberOfLocalAgents = calculateLocalAgents(licenseProperties);
//		this.maximumNumberOfPlans = calculatePlans(licenseProperties);
		this.maximumNumberOfRemoteAgents = MAX_REMOTE_AGENTS_UNLIMITED;
		this.maximumNumberOfLocalAgents = MAX_LOCAL_AGENTS_UNLIMITED;
		this.maximumNumberOfPlans = MAX_PLANS_UNLIMITED;
	}

	public LicenseEdition getLicenseEdition() {
		return this.licenseEdition;
	}

	public int getMaximumNumberOfRemoteAgents() {
		return this.maximumNumberOfRemoteAgents;
	}

	public int getMaximumNumberOfLocalAgents() {
		return this.maximumNumberOfLocalAgents;
	}

	public int getMaximumNumberOfPlans() {
		return this.maximumNumberOfPlans;
	}

	public boolean isUnlimitedRemoteAgents() {
		return -1 == this.maximumNumberOfRemoteAgents;
	}

	public boolean isUnlimitedLocalAgents() {
		return -1 == this.maximumNumberOfLocalAgents;
	}

	public boolean isUnlimitedPlans() {
		return -1 == this.maximumNumberOfPlans;
	}

	private int calculateRemoteAgents(LicenseProperties licenseProperties) {
		String maxRemoteString = licenseProperties
				.getProperty("NumberOfBambooRemoteAgents");
		if ((maxRemoteString != null) && (maxRemoteString.length() != 0)) {
			try {
				return Integer.parseInt(maxRemoteString);
			} catch (NumberFormatException e) {
			}

		}

		if (LicenseType.STARTER.equals(getLicenseType())) {
			return 0;
		}
		if (LicenseEdition.STANDARD.equals(this.licenseEdition)) {
			return 1;
		}
		if (LicenseEdition.PROFESSIONAL.equals(this.licenseEdition)) {
			return 10;
		}
		if (LicenseEdition.ENTERPRISE.equals(this.licenseEdition)) {
			return 25;
		}
		if (LicenseEdition.UNLIMITED.equals(this.licenseEdition)) {
			return 100;
		}

		return 0;
	}

	@SuppressWarnings("unused")
	private int calculateLocalAgents(LicenseProperties licenseProperties) {
		String maxLocalString = licenseProperties
				.getProperty("NumberOfBambooLocalAgents");
		if ((maxLocalString != null) && (maxLocalString.length() != 0)) {
			try {
				return Integer.parseInt(maxLocalString);
			} catch (NumberFormatException e) {
			}

		}

		if (LicenseType.STARTER.equals(getLicenseType())) {
			return -1;
		}
		if (LicenseEdition.BASIC.equals(this.licenseEdition)) {
			return 1;
		}

		return -1;
	}

	@SuppressWarnings("unused")
	private int calculatePlans(LicenseProperties licenseProperties) {
		String maxPlanString = licenseProperties
				.getProperty("NumberOfBambooPlans");
		if ((maxPlanString != null) && (maxPlanString.length() != 0)) {
			try {
				return Integer.parseInt(maxPlanString);
			} catch (NumberFormatException e) {
			}

		}

		if (LicenseType.STARTER.equals(getLicenseType())) {
			return 10;
		}

		return -1;
	}
}